package com.gears_software.bbnetwork.utlis;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.gears_software.bbnetwork.R;
import com.gears_software.common.net.NetworkUtil;
import com.gears_software.common.ui.loadingDislog.LoadingDialog2;

public class CommonUtils {

	static LoadingDialog2 loading = null;

	// 将double保留两位小数
	public static String ratioTransform(double money) {
		BigDecimal moneyDev = new BigDecimal(money).setScale(2,
				RoundingMode.HALF_UP);
		return moneyDev.toString();
	}

	// 将list集合转换成“，”隔开的字符串
	public static String listToString(List<String> stringList) {
		if (stringList == null) {
			return null;
		}
		StringBuilder result = new StringBuilder();
		boolean flag = false;
		for (String string : stringList) {
			if (flag) {
				result.append(",");
			} else {
				flag = true;
			}
			result.append(string);
		}
		return result.toString();
	}

	// 密码验证
	public static boolean isPwd(String pwd) {
		Pattern pattern = Pattern
				.compile("^(?![A-Z]+$)(?![a-z]+$)(?!\\d+$)(?![\\W_]+$)\\S{6,20}$");
		Matcher matcher = pattern.matcher(pwd);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	// 身份验证
	public static boolean isShenfen(String pwd) {
		Pattern pattern = Pattern
				.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
		Matcher matcher = pattern.matcher(pwd);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	// 手机号
	public static boolean isPhone(String pwd) {
		Pattern pattern = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{8}$");
		Matcher matcher = pattern.matcher(pwd);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	// 显示加载动画
	public static void showDialog(Context context) {
		loading = new LoadingDialog2(context, R.style.MyDialog, "加载中...");
		loading.setCanceledOnTouchOutside(false);
		loading.show();
	}

	// 关闭加载动画
	public static void closeDialog() {
		if (loading != null) {
			loading.dismiss();
			loading = null;
		}
	}

	String packageName = "com.tencent.mobileqq";

	public static final boolean isApkInstalled(Context context,
			String packageName) {
		try {
			context.getPackageManager().getApplicationInfo(packageName,
					PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

}
